将数据从对象保存到数据库

您所在的位置:网站首页 visual studio 2022怎么用 将数据从对象保存到数据库

将数据从对象保存到数据库

2023-06-16 16:35| 来源: 网络整理| 查看: 265

将数据从对象保存到数据库 项目 05/04/2023

适用范围:Visual Studio Visual Studio for Mac Visual Studio Code

注意

数据集和相关类是 21 世纪初的旧版 .NET 技术,使应用程序能够在应用程序与数据库断开时处理内存中的数据。 它们对于使用户能够修改数据并持续更改回数据库的应用程序特别有用。 虽然数据集已被证明是一项非常成功的技术,但我们建议新的 .NET 应用程序使用 Entity Framework Core。 实体框架提供了一种更自然的方式来将表格数据作为对象模型,并且具有更简单的编程接口。

通过将对象中的值传递给 TableAdapter 的 DBDirect 方法之一(例如 TableAdapter.Insert),可以将对象中的数据保存到数据库中。 有关详细信息,请参阅 TableAdapter。

若要从对象集合保存数据,请循环访问对象集合(例如 for-next 循环),然后使用 TableAdapter 的 DBDirect 方法之一将每个对象的值发送到数据库。

默认情况下,DBDirect 方法是在可直接针对数据库运行的 TableAdapter 上创建的。 这些方法可直接进行调用,并且不需要 DataSet 或 DataTable 对象来协调更改,以便向数据库发送更新。

注意

配置 TableAdapter 时,主查询必须提供足够的信息,以便创建 DBDirect 方法。 例如,如果将 TableAdapter 配置为从未定义主键列的表中查询数据,则它不会生成 DBDirect 方法。

TableAdapter DBDirect 方法 说明 TableAdapter.Insert 将新记录添加到数据库,使你可以将单个列值作为方法参数传入。 TableAdapter.Update 更新数据库中的现有记录。 Update 方法采用原始列值和新列值作为方法参数。 原始值用于查找原始记录,新值用于更新该记录。

TableAdapter.Update 方法还用于通过将 DataSet、DataTable、DataRow 或 DataRow 的数组作为方法参数,将数据集中的更改协调回数据库。

TableAdapter.Delete 基于作为方法参数传入的原始列值,从数据库中删除现有记录。 将新记录从对象保存到数据库

通过向 TableAdapter.Insert 方法传递值来创建记录。

以下示例通过向 TableAdapter.Insert 方法传递 currentCustomer 对象中的值,在 Customers 表中创建新的客户记录。

C# VB private void AddNewCustomers(Customer currentCustomer) { customersTableAdapter.Insert( currentCustomer.CustomerID, currentCustomer.CompanyName, currentCustomer.ContactName, currentCustomer.ContactTitle, currentCustomer.Address, currentCustomer.City, currentCustomer.Region, currentCustomer.PostalCode, currentCustomer.Country, currentCustomer.Phone, currentCustomer.Fax); } Private Sub AddNewCustomer(ByVal currentCustomer As Customer) CustomersTableAdapter.Insert( currentCustomer.CustomerID, currentCustomer.CompanyName, currentCustomer.ContactName, currentCustomer.ContactTitle, currentCustomer.Address, currentCustomer.City, currentCustomer.Region, currentCustomer.PostalCode, currentCustomer.Country, currentCustomer.Phone, currentCustomer.Fax) End Sub 将现有记录从对象更新到数据库

通过调用 TableAdapter.Update 方法、传入新值以更新记录并传入原始值以查找记录,从而修改记录。

注意

对象需要保留原始值,以便将它们传递给 Update 方法。 此示例使用前缀为 orig 的属性来存储原始值。

以下示例通过向 TableAdapter.Update 方法传递 Customer 对象中的新值和原始值来更新 Customers 表中的现有记录。

C# VB private void UpdateCustomer(Customer cust) { customersTableAdapter.Update( cust.CustomerID, cust.CompanyName, cust.ContactName, cust.ContactTitle, cust.Address, cust.City, cust.Region, cust.PostalCode, cust.Country, cust.Phone, cust.Fax, cust.origCustomerID, cust.origCompanyName, cust.origContactName, cust.origContactTitle, cust.origAddress, cust.origCity, cust.origRegion, cust.origPostalCode, cust.origCountry, cust.origPhone, cust.origFax); } Private Sub UpdateCustomer(ByVal cust As Customer) CustomersTableAdapter.Update( cust.CustomerID, cust.CompanyName, cust.ContactName, cust.ContactTitle, cust.Address, cust.City, cust.Region, cust.PostalCode, cust.Country, cust.Phone, cust.Fax, cust.origCustomerID, cust.origCompanyName, cust.origContactName, cust.origContactTitle, cust.origAddress, cust.origCity, cust.origRegion, cust.origPostalCode, cust.origCountry, cust.origPhone, cust.origFax) End Sub 删除数据库中的现有记录

通过调用 TableAdapter.Delete 方法并传入原始值来查找记录,从而删除记录。

注意

对象需要保留原始值,以便将它们传递给 Delete 方法。 此示例使用前缀为 orig 的属性来存储原始值。

以下示例通过向 TableAdapter.Delete 方法传递 Customer 对象中的原始值,从 Customers 表中删除记录。

C# VB private void DeleteCustomer(Customer cust) { customersTableAdapter.Delete( cust.origCustomerID, cust.origCompanyName, cust.origContactName, cust.origContactTitle, cust.origAddress, cust.origCity, cust.origRegion, cust.origPostalCode, cust.origCountry, cust.origPhone, cust.origFax); } Private Sub DeleteCustomer(ByVal cust As Customer) CustomersTableAdapter.Delete( cust.origCustomerID, cust.origCompanyName, cust.origContactName, cust.origContactTitle, cust.origAddress, cust.origCity, cust.origRegion, cust.origPostalCode, cust.origCountry, cust.origPhone, cust.origFax) End Sub .NET 安全性

必须有权对数据库中的表执行选定的 INSERT、UPDATE 或 DELETE。

另请参阅 将数据保存回数据库


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3